<template>
	<view class="page">
		<view class="user-detail" @tap="userDetail">
			<image :src="DataInfo.Avatar || '/static/images/my/noAvatar.svg'" mode="aspectFill" class="image-avatar"></image>
			<view class="user-info">
				<view class="user-name">
					{{ DataInfo.Name }}
				</view>
				<view v-if="DataInfo.Gongsi">
					{{ DataInfo.Gongsi }}
					<span v-if="DataInfo.Zhiwei">-{{ DataInfo.Zhiwei }}</span>
				</view>
			</view>
		</view>
		<view class="liuyan-box">
			<view class="liuyan-title">对方留言</view>
			<view class="liuyan-text">
				{{ DataInfo.ShenqingLY }}
			</view>
			<view class="time">
				{{ DataInfo.CreateTime }}
			</view>
		</view>
		<view class="btn-container">
			<view class="confirn-btn" @tap="cofirmApply(1)">同意</view>
			<view class="refuse-btn" @tap="cofirmApply(0)">拒绝</view>
		</view>
	</view>
	
</template>

<script>
export default {
	data() {
		return {
			Islogin: this.Microi.IsLogin(),
			CurrentUser: this.Microi.GetCurrentUser(),
			startTime: 0,
			options: {},
			DataInfo: {}
		};
	},

	onLoad(options) {
		this.options = options;
		this.getFriendApply();
	},
	onShow() {
		this.startTime = new Date();
	},
	onHide() {
		if (this.Islogin) {
			// 获取当前页面栈
			const pages = getCurrentPages();
			// 获取当前页面的对象
			const currentPage = pages[pages.length - 1];
			// 获取当前页面的路径
			const route = currentPage.route;
			// 获取当前页面的参数
			const options = currentPage.options;
			this.Microi.ApiEngine.Run('Add_liulanLog', {
				YemianCS: options,
				YemianDZ: route,
				LiulanSJC: new Date().getTime() - this.startTime.getTime()
			});
		}
	},
	onUnload() {
		if (this.Islogin) {
			// 获取当前页面栈
			const pages = getCurrentPages();
			// 获取当前页面的对象
			const currentPage = pages[pages.length - 1];
			// 获取当前页面的路径
			const route = currentPage.route;
			// 获取当前页面的参数
			const options = currentPage.options;
			this.Microi.ApiEngine.Run('Add_liulanLog', {
				YemianCS: options,
				YemianDZ: route,
				LiulanSJC: new Date().getTime() - this.startTime.getTime()
			});
		}
	},
	methods: {
		async getFriendApply() {
			var self = this;
			var res = await self.Microi.ApiEngine.Run('get_FriendApplyDetail', {
				Id: self.options.Id
			});
			self.DataInfo = res.Data;
		},
		userDetail() {
			uni.navigateTo({
				url: '/pages/my/myDetail/myDetail?Id=' + this.DataInfo.YonghuID
			});
		},
		// type == 0 拒绝 1同意
		async cofirmApply(type) {
			var self = this;
			if (type == 0) {
				uni.showModal({
					title: '提示',
					content: '确定拒绝吗～',
					success: async function (res) {
						if (res.confirm) {
							uni.showLoading({
								title: '提交中',
								mask: false
							});
							var res = await self.Microi.ApiEngine.Run('apply_friends', {
								Id: self.options.Id,
								Type: type
							});
							uni.hideLoading();
							self.Microi.Tips(res.Msg, false);
							setTimeout(function () {
								self.goBack();
							}, 1000);
						}
					}
				});
			} else {
				uni.showLoading({
					title: '提交中',
					mask: false
				});
				var res = await self.Microi.ApiEngine.Run('apply_friends', {
					Id: self.options.Id,
					ShenqingRID: self.DataInfo.ShenqingRID,
					BeishenQRID: self.DataInfo.BeishenQRID,
					Type: type
				});
				uni.hideLoading();
				self.Microi.Tips(res.Msg, false);
				setTimeout(function () {
					self.goBack();
				}, 1000);
			}
		},
		goBack() {
			let pages = getCurrentPages(); // 获取当前页面堆栈
			console.log(pages);
			let pageCount = pages.length; // 页面堆栈中的页面数量
			// 获取当前页面的对象
			const currentPage = pages[pages.length - 2];
			// 获取当前页面的路径
			const route = currentPage.route;
			console.log(route, 'route');
			// 判断是否有上级页面
			if (pageCount > 1) {
				// 如果有上级页面，则返回上级页面
				if (route == 'pages/my/TuijianMe/TuijianMe') {
					uni.reLaunch({
						url: '/pages/my/TuijianMe/TuijianMe'
					});
				} else {
					uni.navigateBack({
						delta: 1
					});
				}
			} else {
				// 如果没有上级页面，则返回首页
				uni.switchTab({
					url: '/pages/tabbar/find/find' // 替换为你的首页路径
				});
			}
		}
	}
};
</script>

<style>
	page{
		background-color: #fff;
	}
.page {
	background-color: #fff;
	padding: 20rpx;
	color: #000;
	box-sizing: border-box;
}
.user-detail {
	display: flex;
	align-items: center;
	justify-content: flex-start;
	gap: 32rpx;
}
.image-avatar {
	width: 108rpx;
	height: 108rpx;
	border-radius: 50%;
}
.user-info {
	display: flex;
	justify-content: flex-start;
	flex-direction: column;
	gap: 20rpx;

	font-family: PingFang SC, PingFang SC;
	/* font-weight: 500; */
	font-size: 28rpx;
	color: #666666;
	text-align: left;
	font-style: normal;
	text-transform: none;
}
.user-name {
	font-family: PingFang SC, PingFang SC;
	/* font-weight: 500; */
	font-size: 36rpx;
	color: #000000;
	text-align: left;
	font-style: normal;
	text-transform: none;
}
.liuyan-box {
	margin-top: 82rpx;
	display: flex;
	/* align-items: center; */
	justify-content: flex-start;
	flex-direction: column;
	gap: 26rpx;
}
.liuyan-title {
	font-family: PingFang SC, PingFang SC;
	font-size: 28rpx;
	color: #666666;
	text-align: left;
	font-style: normal;
	text-transform: none;
}
.liuyan-text {
	height: 218rpx;
	background: #f6f6f6;
	border-radius: 16rpx 16rpx 16rpx 16rpx;
	padding: 22rpx 32rpx;

	font-family: PingFang SC, PingFang SC;
	font-size: 32rpx;
	color: #999999;
	text-align: left;
	font-style: normal;
	text-transform: none;
	box-sizing: border-box;
}
.time {
	font-family: PingFang SC, PingFang SC;
	font-size: 24rpx;
	color: #999999;
	text-align: right;
	font-style: normal;
	text-transform: none;
}
.btn-container {
	display: flex;
	align-items: center;
	justify-content: space-between;
	/* flex-direction: column; */
	gap: 16rpx;
	margin-top: 50rpx;
}
.confirn-btn {
	width: 47%;
	height: 68rpx;
	background: #FB353B;
	border-radius: 80rpx 80rpx 80rpx 80rpx;

	font-family: PingFang SC, PingFang SC;
	font-weight: 800;
	font-size: 32rpx;
	line-height: 68rpx;
	color: #ffffff;
	text-align: center;
	font-style: normal;
	text-transform: none;
}
.refuse-btn {
	width: 47%;
	height: 68rpx;
	background: #666;
	border-radius: 80rpx 80rpx 80rpx 80rpx;

	font-family: PingFang SC, PingFang SC;
	font-weight: 800;
	font-size: 32rpx;
	line-height: 68rpx;
	color: #ffffff;
	text-align: center;
	font-style: normal;
	text-transform: none;
}
</style>
